package com.meilai.project.controller.web.personnel.org;

import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.dto.personnel.org.RosterListSearchDTO;
import com.meilai.project.entity.personnel.org.UserMobileRelation;
import com.meilai.project.service.personnel.org.RosterService;
import com.meilai.project.vo.personnel.org.RosterVO;
import com.meilai.project.vo.personnel.org.UserMobileRelationVO;
import io.swagger.annotations.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api(value = "花名册控制器", tags = {"花名册"})
@RestController
@RequestMapping("api/web/roster")
public class WebRosterController {

    @Autowired
    private RosterService service;

    @ApiOperation(value = "列表分页查询", notes = "列表分页查询", response = RosterVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"), @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"), @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"), @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"), @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("list")
    public CommonResult list(@ApiParam(name = "page", value = "当前页", required = true) @RequestParam(value = "page", defaultValue = "1", required = true) Integer page,

                             @ApiParam(name = "page_size", value = "每页数量", required = true) @RequestParam(value = "page_size", defaultValue = "10", required = true) Integer page_size,

                             @ApiParam(name = "type", value = "类型 1:未离职 2:已离职", required = true) @RequestParam("type") Integer type,

                             @ApiParam(name = "name", value = "姓名", required = false) @RequestParam(value = "name", required = false) String name,

                             @ApiParam(name = "dep_ids", value = "部门id集合", required = false) @RequestParam(value = "dep_ids", required = false) List<Long> dep_ids,

                             @ApiParam(name = "job", value = "职务", required = false) @RequestParam(value = "job", required = false) String job,

                             @ApiParam(name = "id_no", value = "身份证号", required = false) @RequestParam(value = "id_no", required = false) String id_no,

                             @ApiParam(name = "mobile", value = "手机号码", required = false) @RequestParam(value = "mobile", required = false) String mobile,

                             @ApiParam(name = "be_regular", value = "是否入编（0：否  1：是）", required = false) @RequestParam(value = "be_regular", required = false) Integer be_regular,

                             @ApiParam(name = "contract_sign_at_min", value = "合同签订时间最小值（yyyy-MM-dd）", required = false) @RequestParam(value = "contract_sign_at_min", required = false) String contract_sign_at_min,

                             @ApiParam(name = "contract_sign_at_max", value = "合同签订时间最大值（yyyy-MM-dd）", required = false) @RequestParam(value = "contract_sign_at_max", required = false) String contract_sign_at_max,

                             @ApiParam(name = "comp_fill_entry_date_min", value = "入编时间最小值（yyyy-MM-dd）", required = false) @RequestParam(value = "comp_fill_entry_date_min", required = false) String comp_fill_entry_date_min,

                             @ApiParam(name = "comp_fill_entry_date_max", value = "入编时间最大值（yyyy-MM-dd）", required = false) @RequestParam(value = "comp_fill_entry_date_max", required = false) String comp_fill_entry_date_max,

                             @ApiParam(name = "years_begin_min", value = "年限开始最小值（yyyy-MM-dd）", required = false) @RequestParam(value = "years_begin_min", required = false) String years_begin_min,

                             @ApiParam(name = "years_begin_max", value = "年限开始最大值（yyyy-MM-dd）", required = false) @RequestParam(value = "years_begin_max", required = false) String years_begin_max,

                             @ApiParam(name = "years_end_min", value = "年限结束最小值（yyyy-MM-dd）", required = false) @RequestParam(value = "years_end_min", required = false) String years_end_min,

                             @ApiParam(name = "years_end_max", value = "年限结束最大值（yyyy-MM-dd）", required = false) @RequestParam(value = "years_end_max", required = false) String years_end_max,

                             @ApiParam(name = "social_security_date_min", value = "参保时间最小值（yyyy-MM-dd）", required = false) @RequestParam(value = "social_security_date_min", required = false) String social_security_date_min,

                             @ApiParam(name = "social_security_date_max", value = "参保时间最大值（yyyy-MM-dd）", required = false) @RequestParam(value = "social_security_date_max", required = false) String social_security_date_max,

                             @ApiParam(name = "social_security_dep_id", value = "参保公司id", required = false) @RequestParam(value = "social_security_dep_id", required = false) Long social_security_dep_id,

                             @ApiParam(name = "sign_confidentiality_agree", value = "保密协议签订（0：否  1：是）", required = false) @RequestParam(value = "sign_confidentiality_agree", required = false) Integer sign_confidentiality_agree,

                             @ApiParam(name = "leave_date_min", value = "离职时间最小值（yyyy-MM-dd）", required = false) @RequestParam(value = "leave_date_min", required = false) String leave_date_min,

                             @ApiParam(name = "leave_date_max", value = "离职时间最大值（yyyy-MM-dd）", required = false) @RequestParam(value = "leave_date_max", required = false) String leave_date_max) {

        RosterListSearchDTO searchDTO = new RosterListSearchDTO();
        if (type != null) {
            searchDTO.setType(type);
        }
        if (StringUtils.isNotEmpty(name)) searchDTO.setName(name);
        if (CollectionUtils.isNotEmpty(dep_ids)) searchDTO.setDep_ids(dep_ids);
        if (StringUtils.isNotEmpty(job)) searchDTO.setJob(job);
        if (StringUtils.isNotEmpty(id_no)) searchDTO.setId_no(id_no);
        if (StringUtils.isNotEmpty(mobile)) searchDTO.setMobile(mobile);
        if (be_regular != null) searchDTO.setBe_regular(be_regular);
        if (StringUtils.isNotEmpty(contract_sign_at_min)) searchDTO.setContract_sign_at_min(contract_sign_at_min);
        if (StringUtils.isNotEmpty(contract_sign_at_max)) searchDTO.setContract_sign_at_max(contract_sign_at_max);
        if (StringUtils.isNotEmpty(comp_fill_entry_date_min))
            searchDTO.setComp_fill_entry_date_min(comp_fill_entry_date_min);
        if (StringUtils.isNotEmpty(comp_fill_entry_date_max))
            searchDTO.setComp_fill_entry_date_max(comp_fill_entry_date_max);
        if (StringUtils.isNotEmpty(years_begin_min)) searchDTO.setYears_begin_min(years_begin_min);
        if (StringUtils.isNotEmpty(years_begin_max)) searchDTO.setYears_begin_max(years_begin_max);
        if (StringUtils.isNotEmpty(years_end_min)) searchDTO.setYears_end_min(years_end_min);
        if (StringUtils.isNotEmpty(years_end_max)) searchDTO.setYears_end_max(years_end_max);
        if (StringUtils.isNotEmpty(social_security_date_min))
            searchDTO.setSocial_security_date_min(social_security_date_min);
        if (StringUtils.isNotEmpty(social_security_date_max))
            searchDTO.setSocial_security_date_max(social_security_date_max);
        if (social_security_dep_id != null) searchDTO.setSocial_security_dep_id(social_security_dep_id);
        if (sign_confidentiality_agree != null) searchDTO.setSign_confidentiality_agree(sign_confidentiality_agree);
        if (StringUtils.isNotEmpty(leave_date_min)) searchDTO.setLeave_date_min(leave_date_min);
        if (StringUtils.isNotEmpty(leave_date_max)) searchDTO.setLeave_date_max(leave_date_max);
        return new CommonResult().success(service.selectPageList(page, page_size, searchDTO));
    }

    @ApiOperation(value = "根据ID获取详情", notes = "根据ID获取详情", response = RosterVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"), @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"), @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"), @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"), @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("/detail")
    public CommonResult detail(@ApiParam(name = "id", value = "主键ID", required = true) @RequestParam("id") Long id) {
        return new CommonResult().success(service.detail(id));
    }


    @ApiOperation(value = "导出员工信息", notes = "导出员工信息")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"), @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"), @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"), @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"), @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @GetMapping("/export_excel")
    public void exportExcel(@ApiParam(name = "ids", value = "员工主键集合", required = true) @RequestParam("ids") List<Long> ids, HttpServletResponse response) throws IOException {
        service.exportExcel(ids, response);
    }

    @GetMapping("list_regular_user")
    @ApiOperation(value = "查询所有正式员工")
    public ApiResult<RosterVO> listRegularUser() {
        List<RosterVO> rosterVOS = service.listRegularList();
        return ApiResult.ok(rosterVOS);
    }

    @GetMapping("/queryUserMobile")
    @ApiModelProperty("查询员工手机")
    public ApiResult<List<UserMobileRelationVO>> queryUserMobile(@RequestParam("user_id") Long user_id) {
        return ApiResult.ok(service.queryUserMobile(user_id));
    }

    @PostMapping("/addOrEdit")
    @ApiModelProperty("查询员工手机")
    public ApiResult<Boolean> addOrEdit(@RequestBody UserMobileRelation relation) {
        return ApiResult.ok(service.addOrEdit(relation));
    }

    @PostMapping("/removeMobile")
    @ApiModelProperty("查询员工手机")
    public ApiResult<Boolean> remove(@RequestParam("relation_id") Long relation_id) {
        return ApiResult.ok(service.remove(relation_id));
    }

    @PostMapping("/verifyMobile")
    @ApiModelProperty("查询员工手机")
    public ApiResult<Boolean> verifyMobile(@RequestParam("relation_id") Long relation_id, @RequestParam("mobile") String mobile) {
        return ApiResult.ok(service.verifyMobile(relation_id, mobile));
    }
}
